﻿/**********************************天使物流*******************************************
* 功能描述:基本信息-线路
* 作者:石锦涛
* 时间:2022-01-08
* 作者2：李周蓬
**********************************天使物流*******************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TianShiProject.EntityFrameworkCore;
using TianShiProject.Models.BaseInfo.Lines;
using TianShiProject.Models.Common;
using Volo.Abp.EntityFrameworkCore;

namespace TianShiProject.BaseInfo.Lines
{
    public class LineAppRepository : DapperDbContextRepository, ILineAppRepository
    {
        public LineAppRepository(IDbContextProvider<TianShiProjectDbContext> dbContextProvider) : base(dbContextProvider)
        {
        }

        /// <summary>
        /// 显示
        /// </summary>
        /// <returns></returns>
        public async Task<List<LineDto>> GetPage()
        {
            try
            {
                PageResponse<LineDto> dos = new PageResponse<LineDto>();
                var sql = "select * from Line";
                return await this.QueryAsync<LineDto>(sql);

            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        ///显示 查询
        /// </summary>
        /// <returns></returns>
        public async Task<List<LineDto>> GetPageId(int id)
        {
            try
            {
                PageResponse<LineDto> dos = new PageResponse<LineDto>();
                var sql = $"select * from Line where Id={id}";
                return await this.QueryAsync<LineDto>(sql);
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 显示线路
        /// </summary>
        /// <param name="name"></param>
        /// <param name="begin"></param>
        /// <param name="end"></param>
        /// <param name="shifou"></param>
        /// <param name="phone"></param>
        /// <param name="hid"></param>
        /// <param name="pageindex"></param>
        /// <param name="pagesize"></param>
        /// <returns></returns>
        public async Task<PageResponse<LineDtos>> GetShowLine(string name, string begin, string end, int shifou, string phone, int hid, int pageindex, int pagesize)
        {
            PageResponse<LineDtos> response = new PageResponse<LineDtos>();
            var sql = $"select a.*,b.OwnerName,b.OwnerPhone,b.OutsourceId,c.OutsourceName from Line a join Owner b on a.OwnerId=b.Id join Outsource c on b.OutsourceId=c.Id where a.IsDelete=0 ";         
            if (!string.IsNullOrWhiteSpace(name))
            {
                sql += $" and a.LineName like '%" + name + "%'";
            }
            if (!string.IsNullOrWhiteSpace(begin))
            {
                sql += $" and a.BeginAddr='{begin}'";
            }
            if (!string.IsNullOrWhiteSpace(end))
            {
                sql += $" and a.EndAddr='{end}'";
            }
            if(shifou>0)
            {
                sql += $" and a.IsOutsource='{shifou}'";
            }
            if (!string.IsNullOrWhiteSpace(phone))
            {
                sql += $" and b.OwnerPhone='{phone}'";
            }
            if (hid >0)
            {
                sql += $" and b.OutsourceId='{hid}'";
            }
            var list = await this.QueryAsync<LineDtos>(sql, null);
            response.PageTotal = list.Count;
            sql += $"order by a.Id offset {(pageindex - 1) * pagesize} rows fetch next {pagesize} rows only";
            var lists = await this.QueryAsync<LineDtos>(sql, null);
            response.PageData = lists;
            return response;
        }
        /// <summary>
        /// 删除线路管理
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<int> DeleteDelLine(int id)
        {
            var sql = $"update Line set IsDelete=1 where Id={id}";
            return await ExecuteAsync(sql, null);
        }
        /// <summary>
        /// 批量删除线路管理
        /// </summary>
        /// <returns></returns>
        public async Task<int> DeleteDelsLine(string ids)
        {
            var list = 0;
            var id = ids.Split(',');
            foreach (var item in id)
            {
                var sql = $"update Line set IsDelete=1 where Id={item}";
                list = await ExecuteAsync(sql, null);
            }
            return list;
        }
        /// <summary>
        /// 编辑修改
        /// </summary>
        /// <param name="LineDtos"></param>
        /// <returns></returns>
        public async Task<int> PutUpdateLine(LineDtos lineDtos)
        {
            var sql = $"update Line set LineName='{lineDtos.LineName}',BeginAddr='{lineDtos.BeginAddr}',EndAddr='{lineDtos.EndAddr}',IsOutsource='{lineDtos.IsOutsource}',Remark='{lineDtos.Remark}',State='{lineDtos.State}',OwnerId='{lineDtos.OwnerId}' where Id={lineDtos.Id}";
            return await ExecuteAsync(sql, null);
        }
        /// <summary>
        /// 添加线路管理
        /// </summary>
        /// <param name="LineDtos"></param>
        /// <returns></returns>
        public async Task<int> PostAddLine(LineDtos lineDtos)
        {
            var sql = $"insert into Line values('{lineDtos.LineNumber}','{lineDtos.LineName}','{lineDtos.BeginAddr}','{lineDtos.EndAddr}',{lineDtos.IsOutsource},'{lineDtos.Remark}',GETDATE(),0,{lineDtos.OwnerId},0)";
            var list = await ExecuteAsync
               (sql, lineDtos);
            return list;
        }
        /// <summary>
        /// 绑定
        /// </summary>
        /// <returns></returns>
        public async Task<PageResponse<OwnerDto>> GetBind()
        {
            PageResponse<OwnerDto> response = new PageResponse<OwnerDto>();
            var sql = $"select * from Owner where IsDelete=0  ";
            response.PageData = await this.QueryAsync<OwnerDto>(sql, null);
            return response;
        }
    }
}
